function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/** 
 * 时间戳转化为年 月 日 时 分 秒 
 * number: 传入时间戳 
 * format：返回格式，支持自定义，但参数必须与formateArr里保持一致 
*/
function formatTimeTwo(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

//只取年月日
function formatTimeThree(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//只取年月
function formatTimeFour(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month].map(formatNumber).join('/')
}
//只取月日
function formatTimeFive(number, format) {

  var formateArr = ['Y', 'M', 'D'];
  var returnArr = [];

  var date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}



function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//生成周
var Week = (function () {
  var ONE_DAY = 24 * 3600 * 1000,
    ONE_WEEK = 7 * ONE_DAY;
  function formatNumber(num) {
    return (num > 9 ? '' : '0') + num;
  }
  function formatDate(date, num) {
    var year = date.getFullYear(),
      month = formatNumber(date.getMonth() + 1),
      day = formatNumber(date.getDate()),
      nextWeek = new Date(+date + ONE_WEEK),
      nextWeekYear = nextWeek.getFullYear(),
      nextWeekMonth = formatNumber(nextWeek.getMonth() + 1),
      nextWeekday = formatNumber(nextWeek.getDate());
    return year + '年 第' + formatNumber(num + 1) + '周 ' + month + '月' + day + '号-' + nextWeekMonth
      + '月' + nextWeekday + '号';
  }
  function Week(year) {
    this.year = new Date(year, 0, 1);
    this.nextYear = new Date(year + 1, 0, 1);
    this.days = 0;
    this.weeks = 0;
  }
  Week.prototype.getDays = function () {
    return this.days = Math.ceil((this.nextYear - this.year) / ONE_DAY);
  }
  Week.prototype.getWeeks = function () {
    return this.weeks = Math.ceil(this.days || this.getDays() / 7);
  }
  Week.prototype.getSomeWeek = function (num) {
    return formatDate(new Date(+this.year + ONE_WEEK * num), num);
  }
  Week.prototype.showWeek = function (num) {
    num = parseInt(num - 1) || 0;
    if (!this.weeks) {
      this.getWeeks();
    }
    if (num > this.weeks) {
      num = 0;
    }
    if (num) {
      return this.getSomeWeek(num);
    } else {
      var arr = [];
      while (num < this.weeks) {
        arr.push(this.getSomeWeek(num));
        num++;
      }
      return arr;
    }
  }
  return Week;
})();





module.exports = {
  formatTime: formatTime,
  formatTimeTwo: formatTimeTwo,
  formatTimeThree: formatTimeThree,
  formatTimeFour: formatTimeFour,
  formatTimeFive: formatTimeFive
}